package com.base.cn.platform.os.service.manage.user;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.user.SysUserFunctionDao;
import com.base.cn.platform.os.entity.manage.user.SysUserFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户权限Biz
 *
 * @author s.li
 * @date 2019/02/14
 */
@Service
@Slf4j
public class SysUserFunctionBiz extends BaseBiz<SysUserFunction, SysUserFunctionDao> {

    /**
     * 查询用户单独授权的权限记录数据
     * @param userId 用户ID
     * @return 记录数据列表
     */
    public List<SysUserFunction> findUserFunctionBuyUserId(BigDecimal userId){
        return this.find(" userId = "+userId,null,null);
    }

    /**
     * 保存用户权限
     * @param userId 用户ID
     * @param functionIds 权限ID串
     */
    @Transactional(rollbackFor =  IllegalArgumentException.class)
    public void saveUserFunction(BigDecimal userId, String functionIds) throws Exception{
        if(StringUtils.isNotEmpty(functionIds)){
            List<SysUserFunction> saveUserFunctionList = new ArrayList<>();
            //查询用户原有的权限数据列表
            List<SysUserFunction> sysUserFunctionList = this.find(" userId = "+userId,null,null);
            String[] idArr = functionIds.split(",");
            Set<BigDecimal> idSet = new HashSet<>();
            Arrays.stream(idArr).forEach(id->{
                idSet.add(new BigDecimal(id));
            });
            if(ObjectUtils.isNotEmpty(sysUserFunctionList)){
                //把用户原有的权限List转成Map
                Map<BigDecimal,SysUserFunction> sysUserFunctionMap = sysUserFunctionList.stream().collect(Collectors.toMap(e->e.getFunctionId(), e->e));


                idSet.forEach(id->{
                    SysUserFunction userFunction = sysUserFunctionMap.get(id);
                    //如果原来有保存过，则不需要再保存了
                    if(userFunction==null){
                        SysUserFunction sysUserFunction = new SysUserFunction();
                        sysUserFunction.setUserId(userId);
                        sysUserFunction.setFunctionId(id);
                        saveUserFunctionList.add(sysUserFunction);
                    }
                });
                if(ObjectUtils.isNotEmpty(saveUserFunctionList)){
                    this.batchSave(saveUserFunctionList);
                }
                //获取不包含的权限记录，用于删除
                List<BigDecimal> deleteId = new ArrayList<>();
                sysUserFunctionList.forEach(e->{
                    if(!idSet.contains(e.getFunctionId())){
                        deleteId.add(e.getId());
                    }
                });
                if(ObjectUtils.isNotEmpty(deleteId)){
                    this.deleteWhereSql(" id in ("+deleteId.stream().map(e->e.toString()).collect(Collectors.joining(","))+")");
                }
            }else{
                idSet.forEach(functionId->{
                    SysUserFunction sysUserFunction = new SysUserFunction();
                    sysUserFunction.setUserId(userId);
                    sysUserFunction.setFunctionId(functionId);
                    saveUserFunctionList.add(sysUserFunction);
                });
                this.batchSave(saveUserFunctionList);
            }
        }else{
            this.deleteWhereSql(" userId = "+userId);
        }
    }
}
